有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何修复此错误?我想建立应用程序加密和解密文件,但显示错误我如何修复它?

我想用java构建加密和解密应用程序,但显示错误,如何修复

我使用AES算法进行加密,我使用java,我如何用这段代码修复它

这是我在CryptoException.java的代码

public class CryptoException extends Exception {
    public CryptoException() {}

    public CryptoException(String message, Throwable throwable) {
        super(message, throwable);
    }
}

这是我在CryptoUtils.java的代码

public class CryptoUtils {
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";

    public static void encrypt(String key, File inputFile, File outputFile)
            throws CryptoException {
        doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);  

    }

    public static void decrypt(String key, File inputFile, File outputFile)
            throws CryptoException {
        doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
    }

    private static void doCrypto(int cipherMode, String key, File inputFile, File outputFile) throws CryptoException {
        try {
            Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(cipherMode, secretKey);

            FileInputStream inputStream = new FileInputStream(inputFile);
            byte[] inputBytes = new byte[(int) inputFile.length()];
            inputStream.read(inputBytes);

            byte[] outputBytes = cipher.doFinal(inputBytes);

            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);

            inputStream.close();
            outputStream.close();

        } catch (NoSuchPaddingException | NoSuchAlgorithmException
                | InvalidKeyException | BadPaddingException
                | IllegalBlockSizeException | IOException ex) {
            throw new CryptoException("Error Encrypting/Decrypting file", ex);
        }   
    }
}

这是我的MainApp.java

public class App {

    public static void main( String[] args ) {
        String key = "Rahasialah";
        File inputFile = new File("D:\\document.txt");
        File encryptedFile = new File("D:\\document.encrypted");
        File decryptedFile = new File("D:\\document.decrypted");

        try {
            CryptoUtils.encrypt(key, inputFile, encryptedFile);
            CryptoUtils.decrypt(key, encryptedFile, decryptedFile);
        } catch (CryptoException ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }
}

这是我的消息错误:

Error Encrypting/Decrypting file
   com.fusi24.bms.EncryptdanDecrypt.CryptoException: Error 

Encrypting/Decrypting file
   at com.fusi24.bms.EncryptdanDecrypt.CryptoUtils.doCrypto(CryptoUtils.java:53)
   at com.fusi24.bms.EncryptdanDecrypt.CryptoUtils.encrypt(CryptoUtils.java:23)
   at com.fusi24.bms.EncryptdanDecrypt.App.main(App.java:20)
   Caused by: java.security.InvalidKeyException: Invalid AES key length: 10 bytes
   at com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:87)
   at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:94)
   at com.sun.crypto.provider.CipherCore.init(CipherCore.java:591)
   at com.sun.crypto.provider.CipherCore.init(CipherCore.java:467)
   at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
   at javax.crypto.Cipher.implInit(Cipher.java:801)
   at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
   at javax.crypto.Cipher.init(Cipher.java:1248)
   at javax.crypto.Cipher.init(Cipher.java:1185)
   at com.fusi24.bms.EncryptdanDecrypt.CryptoUtils.doCrypto(CryptoUtils.java:36)
   ... 2 more

共 (1) 个答案

  1. # 1 楼答案

    您必须按以下方式使用AES加密

    KeyGenerator generator = KeyGenerator.getInstance("AES");
    generator.init(128); // AES key size in number of bits
    SecretKey secKey = generator.generateKey();
    

    如上所述,大小应至少为128位。 然后您必须使用以下代码进行加密

    Cipher aesCipher = Cipher.getInstance("AES");
    aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
    byte[] byteCipherText = aesCipher.doFinal(someTxtToEncrypt.getBytes());